#include <glib.h>

GList *global_list = NULL;

static void _list_str_print_func(gpointer data, gpointer user_data)
{
    g_print("user_data is: %s, data is: %s \n", (gchar *)user_data, (gchar*)data);
}

static void _list_local_var_str_append(void)
{
    gchar str1[32] = {0};
    gchar str2[32] = {0};

    g_strlcpy(str1, "hello", sizeof("hello"));
    g_strlcpy(str2, "world", sizeof("world"));

    global_list = g_list_append(global_list, (gpointer)str1);
    global_list = g_list_append(global_list, (gpointer)str2);
}

static void _list_const_var_str_append(void)
{
    gchar *str1 = "hello";
    gchar *str2 = "world";

    global_list = g_list_append(global_list, (gpointer)str1);
    global_list = g_list_append(global_list, (gpointer)str2);
}

static void _list_heap_var_str_append(void)
{
    gchar *str1 = g_strdup("hello");
    gchar *str2 = g_strdup("world");

    global_list = g_list_append(global_list, (gpointer)str1);
    global_list = g_list_append(global_list, (gpointer)str2);
}

static void _list_heap_var_str_free_func(gpointer data)
{
    g_free((gchar *)data);
}

gint main(gint argc, gchar **argv)
{

    _list_local_var_str_append();
    g_list_foreach(global_list, _list_str_print_func, "local var");
    g_list_free(global_list);
    global_list = NULL;

    _list_const_var_str_append();
    g_list_foreach(global_list, _list_str_print_func, "const var");
    g_list_free(global_list);
    global_list = NULL;

    _list_heap_var_str_append();
    g_list_foreach(global_list, _list_str_print_func, "heap var");
    g_list_free_full(global_list, _list_heap_var_str_free_func);
    global_list = NULL;

    return 0;
}